package com.xx.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xx.entity.Goal;
import com.xx.util.DBHelp;
import com.xx.util.RowMapper;

public class GoalDao {
	DBHelp<Goal> dbHelp = new DBHelp<Goal>();

	public List<Goal> findGoals(String pid) {
		String sql = "select id,name,`desc`,createtime,t_employee_id,t_project_id,state from t_goal where t_project_id=?";
		return dbHelp.executeQueryForList(sql, new Goalmapper(), pid);

	}

	public Goal findGoal(String gid) {
		String sql = "select id,name,`desc`,createtime,t_employee_id,t_project_id,state from t_goal where id=?";
		return dbHelp.executeQueryForObject(sql, new Goalmapper(), gid);

	}

	public void update(Goal goal) {
		String sql = "update t_goal set name= ? ,`desc`=?,state=? where id=?";
		dbHelp.executeSQL(sql, goal.getName(), goal.getDesc(), goal.getState(),
				goal.getId());

	}

	public void insert(Goal goal) {
		String sql = "insert into t_goal(id,name,`desc`,state,createtime,t_employee_id,t_project_id) value(?,?,?,?,?,?,?)";

		dbHelp.executeSQL(sql, goal.getId(), goal.getName(), goal.getDesc(),
				goal.getState(), goal.getCreatetime(), goal.getEmployeeid(),
				goal.getProjectid());

	}

	public class Goalmapper implements RowMapper<Goal> {

		public Goal mapperRow(ResultSet rs) throws SQLException {
			// TODO Auto-generated method stub
			Goal goal = new Goal();
			goal.setId(rs.getString("id"));
			goal.setName(rs.getString("name"));
			goal.setDesc(rs.getString("desc"));
			goal.setEmployeeid(rs.getString("t_employee_id"));
			goal.setProjectid(rs.getString("t_project_id"));
			goal.setState(rs.getString("state"));
			return goal;
		}

	}

	public void del(String gid) {
		String sql="delete from t_goal where id=?";
		dbHelp.executeSQL(sql, gid);
		
	}
}
